local skel = fk.CreateSkill{
  name = "re_heg__huyuan",
}
---@type TrigSkelSpec<EventPhaseChangingFunc>
local huyuan = {
  name = 're_heg__huyuan',
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.Finish and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local success, dat = player.room:askForUseActiveSkill(player, "huyuan_active", "#re_heg__huyuan-choose", true)
    if success then
      self.cost_data = dat
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local dat = self.cost_data
    local choice = dat.interaction
    if choice == "re_heg__huyuan_give" then
      room:obtainCard(dat.targets[1], dat.cards, false, fk.ReasonGive, player.id)
    elseif choice == "re_heg__huyuan_equip" then
      room:moveCardTo(dat.cards, Card.PlayerEquip, room:getPlayerById(dat.targets[1]), fk.ReasonPut, self.name, nil, true, player.id)
      if not player.dead then
        local targets = table.map(table.filter(room.alive_players, function(p)
          return #p:getCardIds("ej") > 0 end), Util.IdMapper)
        local to2 = room:askForChoosePlayers(player, targets, 1, 1, "#re_heg__huyuan_discard-choose", self.name, false, true)
        local cid = room:askForCardChosen(player, room:getPlayerById(to2[1]), "ej", self.name)
        room:throwCard({cid}, self.name, room:getPlayerById(to2[1]), player)
      end
    end
    if not room:getPlayerById(dat.targets[1]).dead and room:getPlayerById(dat.targets[1]):isWounded() then
      if room:getPlayerById(dat.targets[1]).hp <= player.hp then
        room:recover{
          who = room:getPlayerById(dat.targets[1]),
          num = 1,
          recoverBy = player,
          skillName = self.name
        }
      end
    end
  end,
}
skel:addEffect(fk.EventPhaseStart, huyuan)
return skel